<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\User;

use App\Models\UserFile;
use App\Models\UserWalletrecord;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Ramsey\Uuid\Uuid;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * Class SystemUser
 *
 * @property int $id
 * @property string $nickname
 * @property string $username
 * @property string $phone
 * @property string $email
 * @property string $password
 * @property int $parent_id
 * @property float $wallet
 * @property float $integral
 * @property string $openid
 * @property string $avatar_url
 * @property string $province
 * @property string $city
 * @property int $gender
 * @property Carbon $expire_at
 * @property Carbon $last_login
 * @property string $longitude
 * @property string $latitude
 * @property int $level
 * @property int $distribution_level
 * @property string $remember_token
 * @property Carbon $created_at
 * @property Carbon $updated_at
 *
 * @package App\Models
 */
class SystemUser extends Authenticatable implements JWTSubject
{
    use Notifiable;
    public function getJWTIdentifier(){
        return $this->getKey();
    }

    public function getJWTCustomClaims(){
        return [];
    }
	protected $table = 'system_users';
    protected $username = 'phone';
	// 类型转换
	protected $casts = [
		'parent_id' => 'int',
		'wallet' => 'float',
		'integral' => 'float',
		'gender' => 'int',
		'level' => 'int',
		'distribution_level' => 'int'
	];

	protected $dates = [
		'expire_at',
		'last_login'
	];

	protected $hidden = [
//		'password',
		'remember_token'
	];

	protected $fillable = [
	    'no',
	    'uuid',
		'nickname',
		'username',
		'phone',
		'email',
		'password',
		'parent_id',
		'wallet',
		'integral',
		'openid',
		'avatar_url',
		'province',
		'city',
		'gender',
		'expire_at',
		'last_login',
		'longitude',
		'latitude',
		'level',
		'distribution_level',
        'remark' ,
	];
// lang->zh-cn-systemuser
	const COMMENT = [
	    'id' => 'ID',
        'no' =>  '用户编号',
        'uuid' => '唯一标识',
        'nickname'=>'昵称',
        'username' => '账户名',
        'phone' => '手机号',
        'email' => '邮箱',
        'password' => '密码',
        'parent_id' => '父级',
        'wallet' => '余额',
        'integral' => '积分',
        'openid' => '微信openid',
        'avatar_url' => '头像',
        'province' => '省份',
        'city' => '城市',
        'gender' => '性别',
        'expire_at' => '过期时间',
        'last_login' => '最后登录',
        'longitude' => '经度',
        'latitude' => '纬度',
        'level' => '等级',
        'distribution_level' => '分销等级',
        'created_at' => '创建时间',
        'updated_at' => '更新时间',
        'remember_token' => 'token',
        'remark' => '备注'
    ] ;
	protected $NumberIndexStart = 100000;

    /**
     * @return string
     */
    public function getUsername(): string
    {
        return $this->username;
    }
	protected static function boot()
    {
        parent::boot();

       static::creating(function($model){
           $model->uuid = Uuid::uuid1();
           $model->no =  uniqid('auto').rand(1,999999) ;
       });


        static::created(function($model){
            $model->no = $model->makeIndexNumber($model->id) ;
            $model->save();
        });

    }

     /**
     * makeIndexNumber 生成用户编号
     * @time:2020/2/14-15:16
     */
    public  function makeIndexNumber($Index)
    {
        return $this->NumberIndexStart + (int)$Index;
    }

    # relation user_file
    public function Fiels()
    {
        return $this->hasMany(UserFile::class,'user_id','id');
    }

    # 钱包 记录
    public function WalletRecord()
    {
        return $this->hasMany(UserWalletrecord::class, 'user_id', 'id');
    }

    #钱包 提现申请
    public function WithdrawalCashRecord()
    {
        return $this->hasMany(WithdrawalCash::class,'user_id', 'id');
    }



}
